(function ($) {
	var sourceData;
	var perInfo,pageType,nurseid
	var domArr = [{'domId':'AppNurCount','tip':'请输入申请人数','type':'input','key':'AppNurCount'},
			{'domId':'PlanStDate','tip':'请选择调配开始日期','type':'date','key':'PlanStDate'},
			{'domId':'PlanEndDate','tip':'请选择调配结束日期','type':'date','key':'PlanEndDate'},
			{'domId':'PlanDays','tip':'','key':'PlanDays'},
			{'domId':'AppReason','tip':'请输入申请原因','type':'input','key':'AppReason'},
			{'domId':'AppNurCondition','tip':'','type':'input','key':'AppNurCondition'},
			{'domId':'AppSalary','tip':'','type':'input','key':'AppSalary'},
			{'domId':'AppRemark','tip':'','type':'input','key':'AppRemark'}]
	var unUploadFiles=[] //未上传的图片
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	//AppWard|CCU^AppNur|冯亮^AppDate|2022-08-15^AppStatus|未提交^AppNurCount|5^PlanStDate|2022-08-15^PlanEndDate|2022-08-22
	//^PlanDays|7^AppReason|申请原因^AppNurCondition|护士条件^AppSalary|薪酬说明^AppRemark|备注^NoCheck|^NoCheckReason|^RowID|24
	function submitNewData(){
		blurAllInput()
		if ($('#PlanStDate')[0].innerText > $('#PlanEndDate')[0].innerText) {
			mui.toast('开始日期不能晚于结束日期')
			return
		}
		var parr = 'AppWard|'+perInfo['loginWardDesc']+'^AppNur|'+perInfo['loginName']+'^AppDate|'+$('#AppDate')[0].innerText
		for (var i = 0; i < domArr.length; i++) {
			var oneDom = domArr[i]
			var dom = $('#'+oneDom['domId'])[0]
			var domValue = dom.innerText
			if (oneDom['type'] == 'input') {
				domValue = dom.value
			}
			if (domValue == '' && oneDom['tip']!= '') {
				mui.toast(oneDom['tip'])
				return
			}
			parr += '^'+oneDom['key']+'|'+domValue
		}
		parr += '^NoCheck|^NoCheckReason|^RowID|'
		if (sourceData) {
			parr += sourceData['RowID']
		}
		var status = this.getAttribute('data-id');
		//console.log(parr)
		var pdict = {'parr':parr,'flag':status,'id':nurseid}
		var paramDict = {'method':'web.INMPersonComm:SaveTransApp','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var msgcode =result['msgcode']
			if (msgcode == '-1') {
				mui.toast(result['msg'])
				return 
			}
			plus.webview.getWebviewById('../WKPlatformPages/apply/applyList.html').evalJS('detailChanged()')
			mui.back()
		}, function(errorStr) {
			mui.toast(errorStr)
		});
	}
	
	function getTransDetail(){
		var pdict = {'id':sourceData['RowID']};
		//console.log(id + ' ' + method)
		var paramDict = {'method':'web.INMPersonComm:GetTransRec','params':JSON.stringify(pdict),'type':'RecMethod'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataDict = result.data
			updateUI(dataDict)
		}, function(errorStr) {
		 	
		});
	}
	
	function updateUI(dataDict){
		for (var i = 0; i < domArr.length; i++) {
			var oneDom = domArr[i]
			var dom = $('#'+oneDom['domId'])[0]
			dom.innerText = dataDict[oneDom['key']]
			if (oneDom['type'] == 'input') {
				dom.value = dataDict[oneDom['key']]
			}else if (oneDom['domId'] == 'PlanStDate' || oneDom['domId'] == 'PlanEndDate') {
				dom.classList.remove('noTextTip')
			}
		}
		computeDayLength()
	}
	
	function computeDayLength(){
		var startDate = $('#PlanStDate')[0].innerText
		var endDate = $('#PlanEndDate')[0].innerText
		if (startDate.indexOf('日期') != -1 || endDate.indexOf('日期') != -1) {
			return
		}
		var dateLength = $('#PlanDays')[0]
		if (endDate < startDate) {
			dateLength.innerText = ''
			return
		}
		if (startDate == endDate) {
			dateLength.innerText = '1天'
		}
		var start = new Date(startDate).getTime();
		var end = new Date(endDate).getTime();
		var minsec = end - start
		var days = minsec / (24*60*60000) + 1
		dateLength.innerText = days +'天'
	}
	
	//选择日期
	function selectDateNew(){
		blurAllInput()
		var _self = this;
		plus.nativeUI.pickDate(function(e) {
			var d = e.date;
			var dateStr = d.getFullYear()+'-'
			if (d.getMonth() < 9) {
				dateStr += '0'
			}
			dateStr += (d.getMonth() + 1)+'-'
			if (d.getDate() < 10) {
				dateStr += '0'
			}
			dateStr += d.getDate()
			_self.classList.remove('noTextTip')
			_self.innerText = dateStr;
			computeDayLength();
		}, function(e) {}, {});
	}
	
	function getDeviceDateStr(){
		var currentDate = new Date();
		var cyear = currentDate.getFullYear();
		var cmonth = currentDate.getMonth() + 1;
		var cday = currentDate.getDate();
		var thisDay = cyear + "-" 
		if (cmonth < 10) {
			thisDay +='0'
		}
		thisDay+= cmonth + "-" 
		if (cday < 10) {
			thisDay +='0'
		}
		thisDay += cday;
		return thisDay;
	}

	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var self = plus.webview.currentWebview()
		sourceData = self.data
		pageType = self.pageType
		if (sourceData) { //修改
			getTransDetail()
		}
		perInfo = self.perInfo
		nurseid = perInfo['loginID']
		$('#CreatorName')[0].innerText = perInfo['loginName']
		$('#applyLoc')[0].innerText = perInfo['loginWardDesc']
		$('#AppDate')[0].innerText = getDeviceDateStr()
		//关闭按钮
		mui('body').on('tap','#closeBtn',function(){
			blurAllInput()
			mui.back()
		})
		//保存提交按钮
		mui('body').on('tap','.submitBtn',submitNewData)
		// 日期
		mui('#UL').on('tap','.dateType',selectDateNew);
		window.addEventListener('resize',windowResize)
	}
	
})(mui)

